﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Content;
using Momentum.Static;
using Momentum.Types;
using System.Text;

namespace Momentum
{
    class mPlayer
    {
        public int team;
        public mTank tank;
        public mWeapon weapon;
        protected mStaticPlayer.DirectionsFrontal frontalDirection = mStaticPlayer.DirectionsFrontal.None;
        protected mStaticPlayer.DirectionsLateral lateralDirection = mStaticPlayer.DirectionsLateral.None;
        protected mStaticPlayer.DirectionsTurret turretDirection = mStaticPlayer.DirectionsTurret.None;
        protected mStaticPlayer.Action action = mStaticPlayer.Action.None;

        public mTank Tank
        {
            get { return tank; }
            set { tank = value; }
        }

        public int Team
        {
            get { return team; }
            set { team = value; }
        }

        public void ChangeTankType(ContentManager content)
        {
            if (tank.Name == mStaticObject.TankNames[0])
            {
                tank = content.Load<mTank>(@"Tanks/" + mStaticObject.TankNames[1]);
            }
            else
            {
                tank = content.Load<mTank>(@"Tanks/" + mStaticObject.TankNames[0]);
            }
            tank.Position = new Vector2(mStaticWorld.Random.Next((int)mStaticWorld.Size.X), mStaticWorld.Random.Next((int)mStaticWorld.Size.Y));
        }

        public void ChangeTeam()
        {
            team++;
            team %= 2;
        }

        public mPlayer()
        {
            frontalDirection = mStaticPlayer.DirectionsFrontal.None;
            lateralDirection = mStaticPlayer.DirectionsLateral.None;
            turretDirection = mStaticPlayer.DirectionsTurret.None;
            tank = null;
            weapon = null;
            team = 0;
        }

        public mPlayer(mTank t, mWeapon w, int e)
        {
            frontalDirection = mStaticPlayer.DirectionsFrontal.None;
            lateralDirection = mStaticPlayer.DirectionsLateral.None;
            turretDirection = mStaticPlayer.DirectionsTurret.None;
            tank = t;
            weapon = w;
            team = e;
        }

        public Vector2 CameraPosition
        {
            set { tank.CameraPosition = value; weapon.CameraPosition = value; }
        }

        public void Doblar(mStaticPlayer.DirectionsLateral d)
        {
            this.lateralDirection = d;
        }

        public void Advance(mStaticPlayer.DirectionsFrontal d)
        {
            this.frontalDirection = d;
        }

        public void Turret(mStaticPlayer.DirectionsTurret d)
        {
            this.turretDirection = d;
        }

        public void Action(mStaticPlayer.Action a)
        {
            this.action = a;
        }

        public void CalculateFuture()
        {
            tank.CalculateVelocity(frontalDirection, lateralDirection);
            tank.Move(mStaticWorld.Friction);
            tank.UpdatePosition();
            tank.MoveTurret(turretDirection);
            weapon.Update();
        }

        public void Shoot(GameTime gameTime)
        {
            weapon.ToNextShot -= gameTime.ElapsedRealTime.Milliseconds;
            if (action == mStaticPlayer.Action.Shoot && weapon.ToNextShot <= 0)
            {
                action = mStaticPlayer.Action.None;
                weapon.Shoot(tank);
                weapon.ToNextShot = weapon.Delay;
            }
        }

        #region Serialization

        /// <summary>
        /// Serialize the object out to a packet. Le agrego el encabezado.
        /// </summary>
        /// <param name="packetWriter">The packet to write to.</param>
        public void Serialize(PacketWriter packetWriter)
        {
            // safety-check the parameters, as they must be valid
            if (packetWriter == null)
            {
                throw new ArgumentNullException("packetWriter");
            }
            packetWriter.Write((int)mStaticNetwork.PacketTypes.PlayerData);
            packetWriter.Write((int)frontalDirection);
            packetWriter.Write((int)lateralDirection);
            packetWriter.Write((int)turretDirection);
            packetWriter.Write((int)action);
        }

        /// <summary>
        /// Proceso inverso a Serialize. Establece las variables que recibe de el canal de entrada.
        /// Presumo que no tiene el header, ya que otro metodo se lo saco.
        /// </summary>
        /// <param name="packetReader">el lector</param>
        public void Deserialize(PacketReader packetReader)
        {
            if (packetReader == null)
            {
                throw new ArgumentNullException("packetReader");
            }
            frontalDirection = (mStaticPlayer.DirectionsFrontal)packetReader.ReadInt32();
            lateralDirection = (mStaticPlayer.DirectionsLateral)packetReader.ReadInt32();
            turretDirection = (mStaticPlayer.DirectionsTurret)packetReader.ReadInt32();
            action = (mStaticPlayer.Action)packetReader.ReadInt32();
        }

        #endregion
    }
}